package com.why.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.why.entity.EUDataGrid;
import com.why.entity.EasyUiTree;
import com.why.entity.Index;
import com.why.entity.IndexData;
import com.why.form.IndedataForm;
import com.why.service.IndexService;

@Controller
@RequestMapping(value = "/index") 
public class IndexController {

	@Resource
	private IndexService indexService;
	
	@RequestMapping(value = "/getTree.do") 
	public @ResponseBody List<EasyUiTree> indexTree(){
		EasyUiTree en = indexService.getAllIndex();
		List<EasyUiTree> l = new ArrayList<EasyUiTree>();
		l.add(en);
	    return l;
	}
	
	@RequestMapping(value = "/getsubIndexs.do") 
	public @ResponseBody EUDataGrid getIndexInfo(
			@RequestParam("index_code") String index_code){
		EUDataGrid dg = indexService.getIndexByCode(index_code);
		return dg;
	}
	
	@RequestMapping(value = "/editindex.do")
	public @ResponseBody Map<String,String> editIndexinfo(@ModelAttribute Index index){
		String resut = indexService.editIndexInfo(index);
		Map<String,String> map = new HashMap<String,String>();
		map.put("msg", resut);
		return map;
	}
	
	@RequestMapping(value = "/deleteindex.do")
	public @ResponseBody Map<String,String> deleteIndexinfo(
			@RequestParam("id") String id){
		String resut = indexService.deleteIndexInfo(id);
		Map<String,String> map = new HashMap<String,String>();
		map.put("msg", resut);
		return map;
	}
	
	@RequestMapping(value = "/getSub.do") 
	public @ResponseBody Map<String,String> getIndexs(
			@RequestParam("id") String id){
		String str = indexService.getSubIndex(id);
		Map<String,String> map = new HashMap<String,String>();
		map.put("json", str);
		return map;
	}
	
	@RequestMapping(value = "/toedit.do") 
	public @ResponseBody Map<String,String> getIndex(@RequestParam("index_id") String index_id,
			@RequestParam("year") String year,@RequestParam("org_id") String org_id){
		IndexData indexdate = indexService.getIndexDataInfo(index_id, year, org_id);
		Map<String,String> map = new HashMap<String,String>();
		map.put("e_data_value", indexdate.getData_value());
		map.put("e_data_source", indexdate.getData_source());
		map.put("e_id", indexdate.getId());
		map.put("e_index_id", indexdate.getIndex_id());
		map.put("e_org_id", indexdate.getOrg_id());
		map.put("e_year", indexdate.getYear());
		
		return map;
	}
	
	@RequestMapping(value = "/editdata.do")
	public @ResponseBody Map<String,String> editIndexData(@ModelAttribute IndedataForm indexform){
		IndexData indexdate = new IndexData();
		indexdate.setData_source(indexform.getE_data_source());
		indexdate.setData_value(indexform.getE_data_value());
		indexdate.setId(indexform.getE_id());
		indexdate.setIndex_id(indexform.getE_index_id());
		indexdate.setOrg_id(indexform.getE_org_id());
		indexdate.setYear(indexform.getE_year());
		String resut = indexService.editIndexData(indexdate);
		Map<String,String> map = new HashMap<String,String>();
		map.put("msg", resut);
		return map;
	}
	
	@RequestMapping(value = "/saveData.do")
	public void saveIndexData(){
		System.out.println("save index date");
	}
	
	@RequestMapping(value = "/import.do") 
	public void importData(){
		String path="c:/test-1.xls";
		indexService.importToTable(path);
	}
		
		
	@RequestMapping(value = "/template.do") 
	public void exportExcel(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("index_id") String index_id,
			@RequestParam("org_id") String org_id,
			@RequestParam("start") String start){
		
		Workbook wb = indexService.exportToExcel(index_id, org_id, start);
		String filename = start+"年数据导入模板模板";
//		response.setHeader("Content-Disposition","attachment;filename="+filename+".xls");
		response.setContentType("application/vnd.ms-excel");  
//		String codedFileName = null;  
		OutputStream fOut = null; 
		try{
			filename = java.net.URLEncoder.encode(filename, "UTF-8");  
			response.setHeader("content-disposition", "attachment;filename=" + filename + ".xls");  
			fOut = response.getOutputStream();  
			wb.write(fOut);  
		}catch(Exception e){
			e.printStackTrace();
		}finally  {
			try {
				fOut.flush();
				fOut.close();  
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
		}

	}
	
	
}
